package Day08;

public class MyList {
    private Object[] element;

    public MyList() {
    }

    public MyList(Object[] element) {
        this.element = element;
    }

    public Object[] getElement() {
        return element;
    }

    public void setElement(Object[] element) {
        this.element = element;
    }

    void add(String s){//增添
        if(size() == element.length){//判断有效长度与数组长度是否相等，如果相等就得构造一个新的数组，数组名称与最开始数组相等
            Object[] elementNew = new Object[element.length +1];
            for (int i = 0; i < element.length; i++) {
                elementNew[i] = element[i];//把数组值赋给新数组
            }
            elementNew[element.length] = s;//最后一个给新增加的值
            this.element = elementNew;
        }else{
            element[size()] = s;
        }
        System.out.println("存入成功");

    }
    void remove(int index){//删除数组
        if(index > 0 && index < element.length + 1){//判断下标位置是否超出范围
            System.out.println("删除元素为" + element[index - 1]);
            for (int i = index - 1; i < element.length - 2; i++) {
                element[i] = element[i +1];//下标归位，后面的值依次往前面走
            }
            System.out.println("删除成功");
        }else{
            System.out.println("删除异常");
        }

    }
    void get(int index){//获取值
        if(index > 0 && index < element.length + 1){//判断下标是否超出范围
            System.out.println(element[index - 1]);
        }else{
            System.out.println("获取失败");
        }

    }
    int size(){
        int count = 0;//计数器
        for (int i = 0; i < element.length; i++) {
            if(element[i] != null){
                count++;//每有一个不为零的就得加一
            }
        }
        return count;//返回值
    }
}
